package com.qf.sy2103.fmmall.service.impl;

import com.github.pagehelper.PageInfo;
import com.qf.sy2103.common.utils.QfsyBeanUtils;
import com.qf.sy2103.common.vo.ResStatus;
import com.qf.sy2103.common.vo.ResultVO;
import com.qf.sy2103.fmmall.beans.entity.*;
import com.qf.sy2103.fmmall.beans.vo.CategoryVo;
import com.qf.sy2103.fmmall.beans.vo.PageVO;
import com.qf.sy2103.fmmall.beans.vo.ProductCommentsVo;
import com.qf.sy2103.fmmall.beans.vo.ProductVo;
import com.qf.sy2103.fmmall.mapper.*;
import com.qf.sy2103.fmmall.service.ProductService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import com.github.pagehelper.PageHelper;

import java.util.HashMap;
import java.util.List;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class ProductServiceImpl implements ProductService {


    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductImgMapper productImgMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    ProductSkuMapper productSkuMapper;

    @Autowired
    ProductParamsMapper productParamsMapper;

    @Autowired
    ProductCommentsMapper productCommentsMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO listRecommendProducts() {

        final ResultVO resultVO = new ResultVO();

        try{
            final List<ProductVo> productVos = productMapper.selectRecommendProducts();

            resultVO.setData(productVos);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        }catch (Exception e){
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO listRecommendProductsByTk() {
        final ResultVO resultVO = new ResultVO();

        try{

            final Example example = new Example(Product.class);
//            example.orderBy("createTime").desc();
            example.setOrderByClause(" create_time desc limit 0,3");
            final List<Product> products = productMapper.selectByExample(example);
            final List<ProductVo> productVos = QfsyBeanUtils.castToVoList(products, ProductVo.class);
            for (ProductVo productVo : productVos) {
                final ProductImg productImg = new ProductImg();
                productImg.setItemId(productVo.getProductId());
                final List<ProductImg> imgs = productImgMapper.select(productImg);
                productVo.setImgs(imgs);
            }

            resultVO.setData(productVos);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        }catch (Exception e){
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO categoryRecommends() {
        final ResultVO resultVO = new ResultVO();

        try {
            final List<CategoryVo> categoryVos = categoryMapper.categoryRecommends();
            if (categoryVos.size()==0){
                resultVO.setCode(ResStatus.NO);
                resultVO.setMsg("没有查询到任何分类商品数据");
            }else {
                resultVO.setData(categoryVos);
                resultVO.setCode(ResStatus.OK);
                resultVO.setMsg("success");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");

        }


        return resultVO;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailInfo(Integer pid) {

        final ResultVO resultVO = new ResultVO();
        try {
            // 查商品基本信息
            final Product product = productMapper.selectByPrimaryKey(pid);

            // 查商品对应的图片信息
            final Example example = new Example(ProductImg.class);
            final Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("itemId",pid);
            final List<ProductImg> productImgs = productImgMapper.selectByExample(example);

            // 查商品套餐信息
            final ProductSku productSku = new ProductSku();
            productSku.setProductId(pid+"");
            final List<ProductSku> productSkus = productSkuMapper.select(productSku);

            final HashMap<String, Object> map = new HashMap<>();
            map.put("product",product);
            map.put("productImgs",productImgs);
            map.put("productSkus",productSkus);

            resultVO.setData(map);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailParams(Integer pid) {

        final ResultVO resultVO = new ResultVO();

        try {

            final ProductParams condition = new ProductParams();
            condition.setProductId(pid+"");

            final ProductParams productParams = productParamsMapper.selectOne(condition);

            resultVO.setData(productParams);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailCommonts(Integer pid) {
        final ResultVO resultVO = new ResultVO();

        try {

            List<ProductCommentsVo> vos = productCommentsMapper.selectCommontsByProductId(pid);

            resultVO.setData(vos);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailCommonts(Integer pid, Integer pageNum, Integer limit) {
        final ResultVO resultVO = new ResultVO();

        try {
            PageHelper.startPage(pageNum,limit);
            List<ProductCommentsVo> vos = productCommentsMapper.selectCommontsByProductId(pid);
            final PageInfo<ProductCommentsVo> pageInfo = new PageInfo<>(vos);

            PageVO<ProductCommentsVo> pageVO = new PageVO<>();
            pageVO.setCount((int) pageInfo.getTotal());  // 总条数
            pageVO.setPageCount(pageInfo.getPages());  //总页数
            pageVO.setList(vos);

            resultVO.setData(pageVO);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO detailCommontscount(Integer pid) {
        final ResultVO resultVO = new ResultVO();

        try {
            //1.查询当前商品评价的总数
            Example example = new Example(ProductComments.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("productId",pid);
            int total = productCommentsMapper.selectCountByExample(example);

            //2.查询好评评价数
            criteria.andEqualTo("commType",1);
            int goodTotal = productCommentsMapper.selectCountByExample(example);

            //3.查询中评评价数
            Example example1 = new Example(ProductComments.class);
            Example.Criteria criteria1 =  example1.createCriteria();
            criteria1.andEqualTo("productId",pid);
            criteria1.andEqualTo("commType",0);
            int midTotal = productCommentsMapper.selectCountByExample(example1);

            //4.查询差评评价数
            Example example2 = new Example(ProductComments.class);
            Example.Criteria criteria2 =  example2.createCriteria();
            criteria2.andEqualTo("productId",pid);
            criteria2.andEqualTo("commType",-1);
            int badTotal = productCommentsMapper.selectCountByExample(example2);

            //5.计算好评率
            double percent = (Double.parseDouble(goodTotal+"") / Double.parseDouble(total+"") )*100;
            String percentValue = (percent+"").substring(0,(percent+"").lastIndexOf(".")+3);

            HashMap<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("goodTotal",goodTotal);
            map.put("midTotal",midTotal);
            map.put("badTotal",badTotal);
            map.put("percent",percentValue);

            resultVO.setData(map);
            resultVO.setCode(ResStatus.OK);
            resultVO.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            resultVO.setCode(ResStatus.NO);
            resultVO.setMsg("服务器端异常");
        }

        return resultVO;
    }
}
